package com.cmos.um.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

/**
 * @author Feng Yu
 * @version V1.0
 * @date 2017-06-07
 */
public class WSRtnJsonCtx {
	private final static Logger log = LoggerFactory.getLogger(WSRtnJsonCtx.class);
	
	private final static String _OK_CODE = "0";
	//private final static JSONObject _EMPTY_JSON_OBJ = JSONObject.fromObject("{}");
    //private final static JSONArray _EMPTY_JSON_ARR = JSONArray.fromObject("[]");
	
	private JSONObject resJsonObj;

    private String rescode;
    private String exception;
    
    private String method;
    private String output;
    private JSONObject outputObj;
    
    private String success;
    private String contextStr;
    private String contextArrayStr;
    
    public WSRtnJsonCtx() {
		resJsonObj = new JSONObject();
    }
    
    public WSRtnJsonCtx(String jsonTxt) {
		resJsonObj = JSONObject.parseObject(jsonTxt);
		//
		if(resJsonObj != null){
			rescode = resJsonObj.getString("rescode");
			method = resJsonObj.getString("method");
			output = resJsonObj.getString("output");
			outputObj = resJsonObj.getJSONObject("output");
		}
        //
        this.setAttributes();
    }
    
    public void setAttributes(){
    	if(this.isOk()){
        	if(outputObj != null){
        		contextStr = outputObj.getString("context");
        		contextArrayStr = outputObj.getString("contextArray");
        	}
        }else{
			if(outputObj != null){
				exception = outputObj.getString("exception");
			}
            if(StringUtils.isEmpty(rescode)){
				rescode = "-9999";
				exception = "resCode is empty.";
			}
//            log.error(rescode, exception);
//        	throw new ErrorReportException(rescode, exception);
        }
    }
    
    public boolean isOk() {
        return (resJsonObj != null && _OK_CODE.equals(rescode));
    }
    
    public JSONObject getResJsonObj() {
        return resJsonObj;
    }
    
    public JSONObject getOutputObj(){
    	return outputObj;
    }
	
	public JSONObject getContextObj() {
		if(StringUtils.hasText(contextStr)){
			return outputObj.getJSONObject("context");
		}
        return null;
    }
	
	public JSONArray getContextArr(){
		if(StringUtils.hasText(contextArrayStr)){
			return outputObj.getJSONArray("contextArray");
		}
		return null;
	}

	public JSONObject findObjFromArr(){
		if(StringUtils.hasText(contextArrayStr)){
			JSONArray contextArr =  outputObj.getJSONArray("contextArray");
			if(contextArr != null && contextArr.size() > 0){
				return contextArr.getJSONObject(0);
			}
		}
		return null;
	}

    public String getRescode() {
        return rescode;
    }

    public void setRescode(String rescode) {
        this.rescode = rescode;
    }

    public String getException() {
        return exception;
    }

    public void setException(String exception) {
        this.exception = exception;
    }

    public static void main(String[] args) throws Exception {
		//String resStr = "{\"method\":\"listVcc\",\"rescode\":\"0\",\"output\":{\"contextArray\":[{\"vccId\":\"100005\",\"vccName\":\"\"}]}}";
		String resStr = "{\"method\":\"totalVcc\",\"rescode\":\"0\",\"output\":{\"exception\":\"excute sql failed.\"}}";
		//String resStr = "{\"method\":\"totalVcc\",\"rescode\":\"0\",\"output\":{\"totalItem\":\"40\"}}";
		//Long beginTime = System.currentTimeMillis();
		WSRtnJsonCtx ctx = new WSRtnJsonCtx(resStr);
		System.out.println("1."+ctx.getContextObj());
		System.out.println("2."+ctx.getOutputObj());
		System.out.println("3."+ctx.getContextArr());
	}

}
